﻿using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine;
using PloobsEngine.Cameras;
using PloobsEngine.Input;
using Microsoft.Xna.Framework;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Commands;
using PloobsEngineDemo3;
using PloobsEngine.Light;
using PloobsEngine.Modelo.Animation;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Utils;
using System;

namespace Etapa1.Screens
{
    /// <summary>
    /// Screen Animacao
    /// </summary>
    public class MovementScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;

        public MovementScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);                        
        }
        
        public override void LoadContent(EngineStuff es)
        {
            ///Chama o metodo da classe pai
            base.LoadContent(es);

            ///Adiciona o Conponent de Input
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);            

            #region Models

            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cubo");
                sm.SetTexture(TextureCreator.CreateColorTexture(1,Color.Blue),TextureType.DIFFUSE);
                sm.LoadModelo();                
                CustomDeferred shader = new CustomDeferred(false, false, false, false);                
                shader.Initialize();
                IMaterial mat = new Material(shader);
                CharacterControllerInput character = new CharacterControllerInput(this, new Vector3(100, 50, 1),1, (float)Math.Sqrt(2), 50,new Vector3(30));
                character.CharacterController.CorrectCoordinatesForScale = true;
                IObject obj3 = new SimpleObject(mat, character.CharacterController, sm);
                mundo.AddObject(obj3);
            }
            

            ///Cria o cenario padrao de sempre ;)
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero, sm);
                CustomDeferred shader = new CustomDeferred(false, false, false, false);
                shader.SpecularIntensity = 0;
                shader.SpecularPower = 0;
                shader.Initialize();
                IMaterial mat = new Material(shader);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }
            

            #endregion
            ///Camera em primeira pessoa
            cam = new CameraFirstPerson(true);            
            cam.FarPlane = 2000;

            ///Objeto que permite atirar bolas de luzes na cena
            LightThrowBepu lt = new LightThrowBepu(mundo, engine);

            #region NormalLight
            ///Conjunto de luzes direcionais
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);
            //dr.isDebug = true;
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;

            ///Adiciona camera
            mundo.AddCamera(cam);

            ///Conecta o mundo criado com a Screen
            this.World = mundo;
            
        }
    }
}

